#!/bin/bash
#
# 命名规则：-f 查找文件，逐个添加
#           -d 文件路径
#           -p 端口
#
#
Log_HOME=/opt/log
if [ ! -d $Log_HOME ]
then
mkdir -p $Log_HOME
fi
deploy(){

  for I in `ls $HOME`
  do
  if [[ ! "$I" = "pond" && -d $HOME/$I ]]
  then
  echo "deploying $I..."
#  mvn -f $HOME/$I/pom.xml clean compile exec:java -Dexec.mainClass=com.shuimin."$I".App -Dfile.encoding=utf8 2>&1 1>>"$Log_HOME"/"$I".log &
  elif [ ! -f "$HOME/$I/start_work.sh" ]
  then
  echo "$HOME/$I/start_work.sh doesn't exist!"
  continue
  else
  $HOME/$I/start_work.sh -f $HOME/$I 2>&1 1>>"$Log_HOME"/"$I".log &
  echo $!>$Log_HOME/$I.pid
  echo "generate $Log_HOME/$I.pid"
  echo "syslinux port:"
  PORT=`awk 'END {print}' $HOME/$I/start_work.sh|tr '"' '\n'|grep ^[0-9]`
  echo "port $PORT"
#  semanage port -a -t http_port_t -p tcp $PORT
  echo "$I deployed done"
  fi
  done
  echo "all done"
}

killall(){
  for I in `ls $HOME`
  do
  if [[ ! "$I" = "pond" && -d $HOME/$I ]]
  then
  kill -9 `cat $HOME/$I/$I.pid`
  echo "$I killed"
  fi
  done
}
#
#
if [ "$#" -eq 0 ]
then 
echo "Use default directory: '/workspace'"
HOME=/workspace
deploy
fi
while getopts :d:k: opt
do
case $opt in
  d)HOME="$OPTARG" 
  deploy;;
  k)PROJ="$OPTARG"
  if [ $PROJ = "all" ]
  then
  killall
  elif [ -d $HOME/$PROJ ]
  then
  kill -9 $HOME/$PROJ/$PROJ.pid
  else
  echo "no such project!"
  fi
  ;;
  \?)
  echo "用法：写入部署环境根目录不带'/'";;
#  p)PORT="$OPTARG"
#  echo "Your web port is $PORT";;
esac
done
#PRO=${DIRE##*/}
#echo $PRO
#mvn -f "$DIRE"/pom.xml clean compile exec:java -Dexec.mainClass=com.shuimin."$PRO".App -Dfile.encoding=utf8 2>&1 1>>/opt/log/"$PRO".log &
#mvn -f /workspace/szksw/pom.xml clean compile exec:java -Dexec.mainClass=com.shuimin.szksw.App -Dfile.encoding=utf8
